<template>
    <div class="mod-admin__education">
        <div class="top-search">
            
            <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()" label-position="top">
                <div class="top-search_form">
                    <div class="ttop-search_form_input">
                        <el-form-item :label="$t('education.name')">
                            <el-input v-model="state.dataForm.name" :placeholder="$t('education.name')"
                                clearable></el-input>
                        </el-form-item>
                    </div>
                    <div class="top-search_form_btn">
                        <el-form-item>
                            <el-button type="primary" @click="state.getDataList()">{{ $t("query") }}</el-button>
                        </el-form-item>
                    </div>
                </div>
            </el-form>
        </div>
        <div class="content">
            <el-row>
                <el-col :span="4">
                    <div class="flex-sb">
                        <el-tooltip content="新增" placement="top" effect="light">
                            <el-button type="text" icon="el-icon-plus" size="medium" @click="areaAdd"></el-button>
                        </el-tooltip>
                        <el-tooltip content="修改" placement="top" effect="light">
                            <el-button type="text" icon="el-icon-edit" size="medium" @click="areaUpdate"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top" effect="light">
                            <el-button type="text" icon="el-icon-delete" size="medium" @click="areaDelete"></el-button>
                        </el-tooltip>
                    </div>
                    <el-input placeholder="输入关键字进行过滤" v-model="state.filterText">
                    </el-input>
                    <el-tree :data="data" :props="defaultProps" default-expand-all highlight-current
                        ref="treeRef"></el-tree>
                </el-col>
                <el-col :span="1" style="display: flex;justify-content: center;">
                    <el-divider style="height: calc(100vh - 300px)" direction="vertical"></el-divider>
                </el-col>
                <el-col :span="19">
                    <div class="content-btns">
                        <el-button v-if="state.hasPermission('sys:user:save')" type="primary"
                            @click="addOrUpdateHandle()">{{
                                $t("add") }}</el-button>
                        <el-button v-if="state.hasPermission('sys:user:export')" type="info"
                            @click="state.exportHandle()">{{
                                $t("export") }}</el-button>
                    </div>
                    <el-table v-loading="state.dataListLoading" :data="state.dataList"
                        @sort-change="state.dataListSortChangeHandle" style="width: 100%;height: calc(100vh - 390px);">
                        <el-table-column prop="name" :label="$t('education.name')" sortable="custom" header-align="center"
                            align="center"></el-table-column>
                        <el-table-column prop="module" :label="$t('education.module')" sortable="custom"
                            header-align="center" align="center"></el-table-column>
                        <el-table-column prop="status" :label="$t('education.status')" header-align="center" align="center">
                            <template v-slot="scope">
                                <el-tag type="success" v-if="scope.row.status === 1">{{ $t('enable') }}</el-tag>
                                <el-tag type="danger" v-else>{{ $t('stop') }}</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="createDate" :label="$t('education.createDate')" sortable="custom"
                            header-align="center" align="center"></el-table-column>
                        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center"
                            width="150">
                            <template v-slot="scope">
                                <el-button v-if="state.hasPermission('sys:user:update')" type="primary" link
                                    @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
                                <el-button v-if="state.hasPermission('sys:user:delete')" type="primary" link
                                    @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                        :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                        @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle">
                    </el-pagination>
                </el-col>
            </el-row>
        </div>


        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
    </div>
</template>
  
<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs } from "vue";
import AddOrUpdate from "./education-add-or-update.vue";
import { ElMessageBox, ElMessage } from 'element-plus';

const view = reactive({
    getDataListURL: "/edu/tenant/list",
    getDataListIsPage: true,
    deleteURL: "/edu/tenant/delete",
    deleteIsBatch: true,
    exportURL: "/sys/user/export",
    dataForm: {
        name: ""
    },
    filterText: ""
});

const data = reactive([{
    label: '浙江省',
    children: [{
        label: '杭州市',
        children: [{
            label: '上城区'
        },
        {
            label: '西湖区'
        },
        {
            label: '滨江区'
        }
        ]
    },
    {
        label: '金华市',
        children: [{
            label: '浦江县'
        }]
    },
    {
        label: '永康市',
        children: [{
            label: '新市区'
        }]
    }
    ]
}])

const defaultProps = reactive({
    children: 'children',
    label: 'label'
})


const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
    addOrUpdateRef.value.init(id);
};

const treeRef = ref()

const areaAdd = () => {
    ElMessageBox.prompt('请输入区域名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^\\S+$/,
        inputErrorMessage: '请输入'
    }).then(({ value }) => {
    }).catch(() => {
    });
}

const areaUpdate = () => {
    ElMessageBox.prompt('请输入区域名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^\\S+$/,
        inputErrorMessage: '请输入'
    }).then(({ value }) => {
    }).catch(() => {
    });
}

const areaDelete = () => {
    const node = treeRef.value.getCurrentNode();
    console.log(node.label)
    ElMessageBox.confirm('此操作将永久删除该区域, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        ElMessage.success('删除成功!');
    }).catch(() => {
    });
}
</script>
<style lang="less" scoped>
.top-search {
    background-color: #fff;
    padding: 20px 30px 10px;
    border-radius: 10px;
    display: flex;

    /deep/ .el-form {
        width: 100%;
    }

    &_form {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
    }
}

.content {
    background-color: #fff;
    padding: 20px;
    border-radius: 10px;
    margin: 15px 0 0;
    &-btns {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        margin-bottom: 20px;
    }
}
</style>
  